package com.mulanweb.api.module.controller.admin;

import com.jfinal.kit.HashKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.mulanweb.api.module.controller.BaseController;
import com.mulanweb.api.module.model.*;
import com.mulanweb.db.common.SQLGenerateHelper;
import com.mulanweb.validator.annotation.ValidateFile;
import com.mulanweb.validator.annotation.ValidateForm;

import java.util.*;

/**
 * Created by Administrator on 2016/8/19.
 */
@ValidateFile
public class SellerController extends BaseController
{

    @ValidateForm
    public void save()
    {
        String  sellerName  = getPara("sellerName");
        Integer typeId      = getParaToInt("typeId");
        Integer categoryId  = getParaToInt("categoryId");
        Long    cityId      = getParaToLong("cityId");
        Long    parentId    = getParaToLong("parentId");
        String  email       = getPara("email");
        String  mobile      = getPara("mobile");
        String  description = getPara("description");
        String  username    = getPara("username");
        Integer yuyue =getParaToInt("yuyue");
        String  password    = "123456";
        loop:
        if (parentId != null)
        {
            Seller seller = Seller.dao.findById(parentId);
            if (seller != null)
            {
                if (seller.getSellerTypeId() == 26)
                {
                    break loop;
                } else
                {
                    returnJson(0, "父商家不支持创建子商家");
                    return;
                }
            } else
            {
                returnJson(0, "父商家不存在或已删除");
                return;
            }
        }
        Seller seller = Seller.dao.findFirst("select * from seller where seller_name = ?", sellerName);
        if (seller != null)
        {
            returnJson(0, "商家名称已存在，请重新输入");
        } else
        {
            seller = Seller.dao.findFirst("select * from seller where username = ?", username);
            if (seller != null)
            {
                returnJson(0, "商家登陆名已存在，请重新输入");
            } else
            {
                seller = new Seller();
                seller.setSellerName(sellerName);
                seller.setSellerTypeId(typeId);
                seller.setYuyue(yuyue);
                seller.setUsername(username);
                seller.setCategoryId(categoryId);
                seller.setCityId(cityId);
                seller.setParentSeller(parentId);
                seller.setEmail(email);
                seller.setMobileNumber(mobile);
                seller.setDescription(description);
                seller.setCreateTime(new Date());
                seller.setPassword(HashKit.md5("mulan_xxl&seller_login"+password));
                if (seller.save()){
                    returnSuccessJson();
                } else{
                    returnFailureJson();
                }
            }
        }

    }

    public void search()
    {
        String keyWord = getPara("keyWord");
        SQLGenerateHelper helper = new SQLGenerateHelper("seller", "create_time");
        String exceptSelect = "";
        helper.addEqualsCondition("type_id", getParaToLong("typeId"));
        if(keyWord !=null){
            helper.addCondition("seller_name","like",keyWord);
            helper.addCondition("mobile_number","like",keyWord);
            helper.addCondition("email","like",keyWord);
            exceptSelect = helper.getSqlExceptSelect().replace("AND","OR");
        }else{
            exceptSelect = helper.getSqlExceptSelect();
        }
        Page<Seller> page = Seller.dao.paginate(getPageNumber(), getPageSize(), helper.getSqlSelect(),exceptSelect , helper.getQueryParas());
        returnPageModelJson(page);
    }



    public void updatePassword(){
        String oldPassword = getPara("oldPassword");
        String newPassword = getPara("newPassword");
        Long sellerId = getSessionAttr("sellerId");
        Seller seller = Seller.dao.findById(sellerId);
        if(seller !=null && seller.getDeleteTime() == null){
            if(HashKit.md5("mulan_xxl&seller_login"+oldPassword).equals(seller.getPassword())){
                seller.setPassword(HashKit.md5("mulan_xxl&seller_login"+newPassword));
            }else{
                returnJson(0,"原密码错误");
            }
            if(seller.update()){
                returnSuccessJson();
            }else{
                returnFailureJson();
            }
        }else{
            returnJson(0,"商家不合法");
        }
    }

    public void getDetail(){
        Long sellerId = getParaToLong("sellerId");
        Seller seller = Seller.dao.findById(sellerId);
        renderJson(seller);
    }

    public void update(){
        Long sellerId = getParaToLong("sellerId");
        Seller seller = Seller.dao.findById(sellerId);
        if(seller !=null){
            Long cityId = getParaToLong("cityId");
            String email = getPara("email");
            String mobile = getPara("mobile");
            String description = getPara("description");
            seller.setCityId(cityId);
            seller.setDescription(description);
            seller.setMobileNumber(mobile);
            seller.setEmail(email);
            seller.update();
            returnSuccessJson();
        }
    }

}
